#!/usr/bin/env bash

set -e

if [ "$0" != "./build-scripts/dist-recursor" ]; then
  echo "Please run me from the root checkout dir"
  exit 1
fi

if [ -z "$VERSION" ]; then
  VERSION=$(./build-aux/gen-version)
  if [ -z "$VERSION" ]; then
    VERSION="UNKNOWN"
  fi
fi

set -x

cd pdns

ragel dnslabeltext.rl -o dnslabeltext.cc

cd ../docs
pandoc -s -t man manpages/pdns_recursor.1.md -o pdns_recursor.1
pandoc -s -t man manpages/rec_control.1.md -o rec_control.1
cd -

DIST_HOST="$(id -u -n)@$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)"

INCLUDES="iputils.hh arguments.hh base64.hh zoneparser-tng.hh \
rcpgenerator.hh lock.hh dnswriter.hh  dnsrecords.hh dnsparser.hh utility.hh \
recursor_cache.hh rec_channel.hh qtype.hh misc.hh dns.hh syncres.hh \
sstuff.hh mtasker.hh mtasker.cc lwres.hh logger.hh pdnsexception.hh \
mplexer.hh pubsuffix.hh \
dns_random.hh lua-pdns.hh lua-recursor.hh namespaces.hh \
recpacketcache.hh base32.hh cachecleaner.hh json.hh version.hh \
ws-recursor.hh ws-api.hh secpoll-recursor.hh \
responsestats.hh webserver.hh dnsname.hh"

CFILES="syncres.cc iputils.cc  misc.cc unix_utility.cc qtype.cc \
logger.cc arguments.cc  lwres.cc pdns_recursor.cc lua-iputils.cc \
recursor_cache.cc  dnsparser.cc dnswriter.cc  dnsrecords.cc  rcpgenerator.cc  \
base64.cc  zoneparser-tng.cc  rec_channel.cc rec_channel_rec.cc rec_control.cc \
selectmplexer.cc epollmplexer.cc kqueuemplexer.cc portsmplexer.cc pdns_hw.cc \
sillyrecords.cc pubsuffix.cc lua-pdns.cc lua-recursor.cc randomhelper.cc \
devpollmplexer.cc recpacketcache.cc dns.cc reczones.cc base32.cc nsecrecords.cc \
dnslabeltext.cc json.cc ws-recursor.cc ws-api.cc version.cc dns_random.cc \
responsestats.cc webserver.cc rec-carbon.cc secpoll-recursor.cc dnsname.cc"

./mkpubsuffixcc

DIRNAME=pdns-recursor-$VERSION
rm -rf $DIRNAME
mkdir  $DIRNAME
cp $INCLUDES $CFILES $DIRNAME
cp Makefile-recursor $DIRNAME/Makefile.in
cp README-recursor $DIRNAME/README
cp ../COPYING ../NOTICE $DIRNAME/
cp config-recursor.h $DIRNAME/config.h
cat >>$DIRNAME/config.h <<EOF
#define VERSION "$VERSION"
#define DIST_HOST "$DIST_HOST"
#define HAVE_BOOST 1
EOF
mkdir -p $DIRNAME/ext/rapidjson/include/rapidjson/internal
cp -a ../ext/rapidjson/include/rapidjson/*.h $DIRNAME/ext/rapidjson/include/rapidjson/
cp -a ../ext/rapidjson/include/rapidjson/internal/*.h $DIRNAME/ext/rapidjson/include/rapidjson/internal
mkdir -p $DIRNAME/ext/polarssl/include/polarssl
cp -a ../ext/polarssl/include/polarssl/config.h ../ext/polarssl/include/polarssl/check_config.h ../ext/polarssl/include/polarssl/aes.h ../ext/polarssl/include/polarssl/base64.h ../ext/polarssl/include/polarssl/platform.h ../ext/polarssl/include/polarssl/version.h $DIRNAME/ext/polarssl/include/polarssl
mkdir -p $DIRNAME/ext/polarssl/library
cp -a ../ext/polarssl/library/aes.c ../ext/polarssl/library/base64.c $DIRNAME/ext/polarssl/library
cp -a ../ext/yahttp/ $DIRNAME/ext/yahttp
echo '#include "../../../config.h"' > $DIRNAME/ext/yahttp/yahttp/yahttp-config.h
mkdir $DIRNAME/rrd
cp tools/rrd/{create,update,makegraphs,index.html} $DIRNAME/rrd
cp dnslabeltext.rl $DIRNAME
cp pdns-recursor.init.d $DIRNAME
cp ../docs/pdns_recursor.1 ../docs/rec_control.1 $DIRNAME
cp configure-recursor $DIRNAME/configure
cp powerdns-example-script.lua $DIRNAME
mkdir -p $DIRNAME/contrib
cp ../contrib/systemd-pdns-recursor.service $DIRNAME/contrib
mkdir -p $DIRNAME/build-scripts/redhat
cp ../build-scripts/build-recursor-semistatic $DIRNAME/build-scripts
cp ../build-scripts/redhat/pdns-recursor-test.spec $DIRNAME/build-scripts/redhat
cp pdns-recursor.spec $DIRNAME

mkdir -p $DIRNAME/sysdeps
rm -f sysdeps-recursor/*~
cp  sysdeps-recursor/* $DIRNAME/sysdeps
touch $DIRNAME/dnslabeltext.cc # avoid spurious recompiles
tar cf $DIRNAME.tar $DIRNAME
bzip2 -f $DIRNAME.tar # .. solaris
